<template>
	<view class="page-main">
		<s-choose-address ref="liuChooseAddress" @submit="submitAddress" @detele="deteleAddress">
		</s-choose-address>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				address_id:'',
				addressData: {
					type:'add',
					name: '',
					mobile: '',
					address: '',
					lng: '',
					lat: '',
					province:'',
					city:'',
					district:'',
					street:'',
					default: 2
				}
			}
		},
		onLoad(option) {
			this.addressData.type = option.type;
			if(option.type == 'add') {
				uni.setNavigationBarTitle({
					title:'添加收货地址'
				})
			} else if(option.type == 'edit') {
				uni.setNavigationBarTitle({
					title:'编辑收货地址'
				})
				this.address_id = option.id;
				this.getAddressDetail(option.id)
			}
		},
		methods: {
			async getAddressDetail(address_id) {
				let res = await this.$api.getAddressDetail({
					address_id: address_id || this.address_id
				});
				if (res.code == 1) {
					let data = res.data;
					this.addressData.name = data.name
					this.addressData.mobile = data.mobile
					this.addressData.address = data.address
					this.addressData.default = data.default
					this.addressData.province = data.province
					this.addressData.city = data.city
					this.addressData.district = data.district
					this.addressData.id = address_id || this.address_id
					this.addressData.lng = data.lng
					this.addressData.lat = data.lat
					this.address = data.province + data.city + data.district
					this.editAddress(this.addressData)
				}
			},
			//添加地址成功回调
			async submitAddress(e) {
				console.log('添加调地址信息：', e)
				this.addressData = {
					type:this.addressData.type,
					id:e.id,
					name: e.name,
					mobile: e.mobile,
					address: e.detailAddress,
					lng: e.longitude,
					lat: e.latitude,
					province:e.province,
					city:e.city,
					district:e.district,
					street:'',
					default: e.agreeState ? 1 : 2
				}
				let res = await this.$api.addAddress(this.addressData);
				if (res.code == 1) {
					this.$wanlshop.msg(this.addressData.type == 'add' ? '添加成功' : '保存成功')
					setTimeout(() => {
						uni.navigateBack()
					}, 1500);
				}
			},
			//删除地址回调
			deteleAddress(e) {
				console.log('所删除的地址信息：', e)
				uni.showModal({
					title: '提示',
					content: '是否确认删除该地址？',
					success: async (res) => {
						if (res.confirm) {
							let res = await this.$api.deladdress({
								address_id:e.id
							})
							if(res.code == 1) {
								this.$wanlshop.msg('删除成功');
								setTimeout(() => {
									uni.navigateBack()
								}, 1500);
							}
						}
					}
				});
			},
			// 地址回显(编辑)调用此方法
			editAddress(data) {
				let obj = {
					id: data.id,
					detailAddress: data.address, //门牌号(详细地址)
					name: data.name, //联系人姓名
					mobile: data.mobile, //手机号
					title: data.address, //地址概述
					address: data.province + data.city + data.district, //详细地址
					longitude: data.lng, //经度
					latitude: data.lat, //纬度
					agreeState: data.default == 1 ? true : false, //是否是默认地址
					province: data.province,
					city: data.city,
					district: data.district
				}
				this.$refs.liuChooseAddress.setData(obj)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page-main {
		width: 100%;
		height: 100vh;
	}
</style>